package com.zucc.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.zucc.controller.req.NoticeAddReq;
import com.zucc.controller.req.NoticeModifyReq;
import com.zucc.controller.req.NoticeSearchReq;
import com.zucc.controller.resp.NoticeResp;
import com.zucc.entity.Notice;
import com.zucc.entity.NoticeMapper;
import com.zucc.entity.base.MyMapper;
import com.zucc.exception.SyzxException;
import com.zucc.service.NoticeService;
import com.zucc.service.base.MyServiceImpl;
import com.zucc.service.base.PageData;
import com.zucc.util.MyPageUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @author : charspan
 * @Description :
 * @create : 2021-12-05 4:52 下午
 */
@Service
public class NoticeServiceImpl extends MyServiceImpl<Notice> implements NoticeService {

    @Resource
    private NoticeMapper noticeMapper;

    @Override
    public MyMapper<Notice> mapper() {
        return noticeMapper;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(NoticeAddReq req) {
        Notice notice = new Notice();
        BeanUtil.copyProperties(req, notice);
        notice.setDelFlag(0);
        notice.setCreateTime(DateUtil.date());
        notice.setModifyTime(DateUtil.date());
        super.saveKey(notice);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void modify(NoticeModifyReq req) {
        Notice noticeExist = super.findById(req.getId());
        if (Objects.isNull(noticeExist) || noticeExist.getDelFlag().intValue() == 1) {
            throw new SyzxException("系统找不到相应编号的通知信息");
        }
        BeanUtil.copyProperties(req, noticeExist, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
        noticeExist.setModifyTime(DateUtil.date());
        super.updateSelective(noticeExist);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteOne(Long id) {
        Notice noticeExist = super.findById(id);
        if (Objects.isNull(noticeExist) || noticeExist.getDelFlag().intValue() == 1) {
            throw new SyzxException("系统找不到相应编号的通知信息");
        }
        noticeExist.setDelFlag(1);
        noticeExist.setDeleteTime(DateUtil.date());
        super.updateSelective(noticeExist);
    }

    @Override
    public PageData<NoticeResp> page(NoticeSearchReq req) {
        Example example = new Example(Notice.class);
        example.setOrderByClause("modify_time DESC");
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("delFlag", 0);
        if (Objects.nonNull(req.getSysTypeDictItemId())) {
            criteria.andEqualTo("sysTypeDictItemId", req.getSysTypeDictItemId());
        }
        if (Objects.nonNull(req.getType())) {
            criteria.andEqualTo("type", req.getType());
        }
        if (StrUtil.isNotBlank(req.getTitle())) {
            criteria.andLike("title", "%" + req.getTitle() + "%");
        }
        if (StrUtil.isNotBlank(req.getContent())) {
            criteria.andLike("content", "%" + req.getContent() + "%");
        }
        if (StrUtil.isNotBlank(req.getSender())) {
            criteria.andLike("sender", "%" + req.getSender() + "%");
        }
        if (StrUtil.isNotBlank(req.getSendDate())) {
            criteria.andLike("sendDate", "%" + req.getSendDate() + "%");
        }
        PageData<Notice> pageData = super.findByPage(req, example);
        PageData<NoticeResp> convert = MyPageUtil.convert(pageData, NoticeResp.class);
        return convert;
    }
}
